#include "EditTRDPTestCase.h"
#include <QMessageBox>
#include <qdebug.h>

EditTRDPTestCase::EditTRDPTestCase(QWidget* parent)
	: QWidget(parent)
{
	ui.setupUi(this);
	connectAllSlots();
    dataManager.LoadDataFromExcel();
    ShowComID();
}

EditTRDPTestCase::~EditTRDPTestCase()
{
qDebug() << __FUNCTION__;
}

void EditTRDPTestCase::connectAllSlots()
{
	connect(ui.pushButton_addModifyValue, &QPushButton::clicked, this, &EditTRDPTestCase::OnPushbuttonVarientModifyClicked);
	connect(ui.pushButton_delete, &QPushButton::clicked, this, &EditTRDPTestCase::OnPushbuttonDeleteClicked);
	connect(ui.listWidget_comid, &QListWidget::currentItemChanged, this, &EditTRDPTestCase::OnComidListWdigetChange);
}


void EditTRDPTestCase::ShowComID()
{
	QList<QString> listData;

	int nkeys = dataManager.TRDPDatas.keys().count();
    for (int i = 0; i < nkeys; i++)
	{
		QString strComid = QString::number(dataManager.TRDPDatas.keys()[i]);
		listData.append(strComid);
	}

	AddDataToListWidget(ui.listWidget_comid, listData);

}

void EditTRDPTestCase::OnComidListWdigetChange(QListWidgetItem* current)
{
	//添加comID改变事件
	QString strComID = current->text();
    ui.lineEdit_comID->setText(strComID);
	unsigned int nComid = strComID.toUInt();
	QList<QString> listData;

	int ndatasize = dataManager.TRDPDatas[nComid].count();
    for (int i = 0; i < ndatasize; i++)
	{
		TRDP_ExcelDataFormat comidData = dataManager.TRDPDatas[nComid].at(i);
		listData.append(comidData.VariableName);
	}

	AddDataToListWidget(ui.listWidget_Varible, listData);
}

void EditTRDPTestCase::OnPushbuttonVarientModifyClicked()
{
	QString text = ui.lineEdit_ValueToSet->text();
	if (text.isEmpty())
	{
		return;
	}
    if(ui.lineEdit_timespan->text().isEmpty())
    {
        return;
    }
    if(ui.lineEdit_DestIP->text().isEmpty())
    {
        return;
    }

    if(ui.lineEdit_sourceIP->text().isEmpty())
    {
        return;
    }
	//获取变量名
	QString strVariantName = ui.listWidget_Varible->currentItem()->text();

	QString strCombine = strVariantName + "=" + text;
    modifyResult.cycleTime = ui.lineEdit_timespan->text().toUInt();
    modifyResult.ComID=ui.lineEdit_comID->text().toInt();
    modifyResult.strDestIp = ui.lineEdit_DestIP->text();
    modifyResult.strSourceIP = ui.lineEdit_sourceIP->text();
    modifyResult.VariantName_Value.append(strCombine);
    AddDataToListWidget(ui.listWidget_VaribleSetResult, modifyResult.VariantName_Value);
}

void EditTRDPTestCase::OnPushbuttonDeleteClicked()
{
	QString strDelete = ui.listWidget_VaribleSetResult->currentItem()->text();

    for (int i = modifyResult.VariantName_Value.size() - 1; i >= 0; --i)
	{
        if (modifyResult.VariantName_Value[i] == strDelete)
		{
            modifyResult.VariantName_Value.removeAt(i);

		}
	}
    AddDataToListWidget(ui.listWidget_VaribleSetResult, modifyResult.VariantName_Value);

}

void EditTRDPTestCase::ClearModifyResult()
{

}

TRDP_ModifyResult EditTRDPTestCase::GetTRDPModifyResult() const
{
    return modifyResult;
}

void EditTRDPTestCase::AddDataToListWidget(QListWidget* listWidget, const QList<QString>& listData)
{
	listWidget->clear();
    for (int i = 0; i < listData.count(); i++)
	{
		if (listData[i] == QString::fromLocal8Bit("Spare") || listData[i] == QString::fromLocal8Bit("spare"))
		{
			continue;
		}
		QListWidgetItem* item = new QListWidgetItem();
		//设置大小
		item->setSizeHint(QSize(listWidget->width(), 50));
		//设置内容
		item->setText(QString::fromLocal8Bit("%1").arg(listData[i]));
		//设置属性
		item->setFlags(Qt::ItemIsSelectable | Qt::ItemIsEnabled);//双击可被编辑，可选中
		listWidget->addItem(item);
	}
}

void EditTRDPTestCase::on_pushButton_createProcedure_clicked()
{
    this->hide();
    //传递编辑结果给父窗口
    emit OnModifyFinish();
}
